﻿using AutoMapper;
using MediatR;
using S_Gridding.Api.Application.Commands.MeshType;
using S_Gridding.Domains.BasicData;
using S_Gridding.Infrastructure.Interface;
using S_Gridding.Infrastructure.Interface.IWrite;

namespace S_Gridding.Api.Application.CommandsHanders.MeshTypeHanders
{
    /// <summary>
    /// SqlSugar 中介者模式添加网格类型管理表
    /// 需要下载包然后Program注入(下载两个包)
    /// </summary>
    public class MeshTypeSqlSuageCeateCommandHander : IRequestHandler<MeshTypeSqlSuageCeateCommand, int>
    {
        private readonly IMeshTypeRepository _meshTypeRepository; //网格类型管理表仓储接口
        private readonly IMapper _mapper;  //automapper使用

        public MeshTypeSqlSuageCeateCommandHander(IMeshTypeRepository meshTypeRepository, IMapper mapper)
        {
            _meshTypeRepository = meshTypeRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// SqlSuage框架  使用中介者添加网格类型管理信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(MeshTypeSqlSuageCeateCommand request, CancellationToken cancellationToken)
        {
            var res = _mapper.Map<MeshTypeInfo>(request);
            return await _meshTypeRepository.AddAsync(res);
        }
    }
}
